package ReviewAlgorithms.greedyThought;

public class CanPlaceFlowers {

    public static void main(String[] args) {
        int[] flowerbed = {1,0,0,0,0,1};
        int n = 2;
        System.out.println(canPlaceFlowers(flowerbed,n));
    }

    private static boolean canPlaceFlowers(int[] flowerbed, int n) {
        if (flowerbed == null || flowerbed.length < 1){
            return false;
        }
        if (n == 0) {
            return true;
        }
        int count = 1;
        int sum = 0;
        for (int i = 0; i < flowerbed.length; i++) {
            if (flowerbed[i] == 0){
                count ++;
            }
            if (i == flowerbed.length - 1 || flowerbed[i] == 1) {
                if (flowerbed[i] == 0 && i == flowerbed.length - 1){
                    count ++;
                }
                if (count >= 1){
                    sum += (count - 1)/2;
                    count = 0;
                }
            }
        }
        return sum >= n;
    }
}
